package com.yykj.user.center.server.service.impl.patient;

import com.yykj.common.service.RedisService;
import com.yykj.common.util.GsonUtil;
import com.yykj.common.util.LocalRequestUtil;
import com.yykj.common.util.LocalStringUtil;
import com.yykj.user.center.server.dto.AddressDTO;
import com.yykj.user.center.server.dto.UserLoginInfo;
import com.yykj.user.center.server.form.AddressForm;
import com.yykj.user.center.server.form.DeleteAddressForm;
import com.yykj.user.center.server.form.UpdateAddressForm;
import com.yykj.user.center.server.model.Address;
import com.yykj.user.center.server.repository.patient.AddressRepository;
import com.yykj.user.center.server.service.patient.AddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenl
 * @version 1.0
 * @date 2021/11/5 21:10
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressRepository addressRepository;
    @Autowired
    private RedisService redisService;
    @Override
    public AddressDTO info(String id) {
        Address address = addressRepository.findById(id).orElse(null);
        AddressDTO addressDTO = new AddressDTO();
        if(address!=null){
            BeanUtils.copyProperties(address,addressDTO);
        }
        return addressDTO;
    }

    @Override
    public Boolean update(UpdateAddressForm updateAddressForm) {
        String token = LocalRequestUtil.getToken();
        UserLoginInfo userLoginInfo = GsonUtil.fromJson(redisService.get(token),UserLoginInfo.class);
        String userId = userLoginInfo.getUserId();
        if(updateAddressForm.getIsDefault()){
            List<Address> addresses = addressRepository.findAllByAccountIdAndIsDefaultAndState(userId,true,true);
            for(Address addr:addresses){
                addr.setIsDefault(false);
                addressRepository.save(addr);
            }
        }
        Address address = addressRepository.findById(updateAddressForm.getId()).orElse(null);
        BeanUtils.copyProperties(updateAddressForm,address);
        address.setUpdateTime(LocalDateTime.now());
        addressRepository.save(address);
        return true;
    }

    @Override
    public Boolean insert(AddressForm addressForm) {
        Address address = new Address();
        BeanUtils.copyProperties(addressForm,address);
        String token = LocalRequestUtil.getToken();
        UserLoginInfo userLoginInfo = GsonUtil.fromJson(redisService.get(token),UserLoginInfo.class);
        String userId = userLoginInfo.getUserId();
        if(addressForm.getIsDefault()){
            List<Address> addresses = addressRepository.findAllByAccountIdAndIsDefaultAndState(userId,true,true);
            for(Address addr:addresses){
                addr.setIsDefault(false);
                addressRepository.save(addr);
            }
        }
        address.setAccountId(userId);
        address.setId(LocalStringUtil.getUUID());
        address.setState(true);
        addressRepository.save(address);
        return true;
    }

    @Override
    public List<AddressDTO> list(String patientCardId) {
        String token = LocalRequestUtil.getToken();
        UserLoginInfo userLoginInfo = GsonUtil.fromJson(redisService.get(token),UserLoginInfo.class);
        String userId = userLoginInfo.getUserId();
        List<Address> addresses = new ArrayList<>();
        List<AddressDTO> addressesDTO = new ArrayList<>();
        if(StringUtils.isNotBlank(patientCardId)){
            addresses = addressRepository.findAllByAccountIdAndPatientCardIdAndStateOrderByCreateTimeDesc(userId,patientCardId,true);
        }else{
            addresses = addressRepository.findAllByAccountIdAndStateOrderByCreateTimeDesc(userId,true);
        }
        for(Address address:addresses){
            AddressDTO addressDTO = new AddressDTO();
            BeanUtils.copyProperties(address,addressDTO);
            addressesDTO.add(addressDTO);
        }
        return addressesDTO;
    }

    @Override
    public Boolean delete(DeleteAddressForm deleteAddressForm) {
        Address address = addressRepository.findById(deleteAddressForm.getId()).orElse(null);
        if(address!=null){
            address.setState(false);
            addressRepository.save(address);
        }
        return true;
    }
}
